<?php
namespace Home\Controller;
use Think\Controller;
class ApiController extends Controller {
    //新增用户
    public function Numberaddapi(){
        $openid = I('param.uid');
        $nickname = I('param.wx_name');
        $initcash = I('param.init_card', 0, 'intval');
        $Gamer = M('Gamer');
        if ($openid == "") {
            $result = array(
                'resultcode' => 40001,
                'data' => '玩家id为空！',
            );
            print json_encode($result);
        } else if ($nickname == "") {
            $result = array(
                'resultcode' => 40002,
                'data' => '玩家昵称为空！',
            );
            print json_encode($result);
        } else if ($openid != "" and $nickname != "") {
            $re = $Gamer->where('openid=' . $openid)->find();
            if ($re) {
                $result = array(
                    'resultcode' => 40003,
                    'reason' => '已存在相同的openid！',
                );
                print json_encode($result);
            } else {

                
                $data['openid'] = $openid;
                $data['nickname'] = $nickname;
                $data['init_pay'] = $initcash;
                $data['addtime'] = time();
                $r=$Gamer->data($data)->add();
                if ($r) {
                    $result = array(
                        'resultcode' => 40000,
                        'reason' => '导入成功！',
                    );
                    print json_encode($result);
                }else{
                    $result = array(
                        'resultcode' => 40004,
                        'reason' => '导入失败！',
                    );
                    print json_encode($result);
                }
                
            }
        } else {
            $result = array(
                'resultcode' => 40004,
                'reason' => '用户信息导入失败！',
            );
            print json_encode($result);
        }
    }

	//开房间消费房卡接口
    public function Roomapi(){
        $data['openid'] = I('param.uid');
        $data['num'] = I('param.card');
        $data['type']=I('param.type', 1, 'intval');
        $data['addtime']=time();
        $re=M('Consume')->add($data);
        if ($re) {
            //总消费数累加
            M('Gamer')->where('openid='.$data['openid'])->setInc('total_consume',$data['num']);
            $result = array(
                'resultcode' => 40000,
                'reason' => '记录成功！',
            );
            print json_encode($result);
        }else{
            $result = array(
                'resultcode' => 40004,
                'reason' => '记录失败！',
            );
            print json_encode($result);
        }
    }
    //公告接口
    public function Noticeapi(){
        $type=I('param.type', 1, 'intval');
        $bOb = M('Bulletin');
        $re = $bOb->field('id,title,content,type')->where('type='.$type.' and status=0')->order('id desc')->find();
        $result = array(
            'data' => array_slice($re, 0, 4)
        );
        echo (json_encode($result));
    }
    //道具购买消费房卡接口
    public function Propapi(){
        $data['openid'] = I('param.uid');
        $data['num'] = I('param.card');
        $data['type']=I('param.type', 2, 'intval');
        $data['addtime']=time();
        $re=M('Consume')->add($data);
        if ($re) {
            //总消费数累加
            M('Gamer')->where('openid='.$data['openid'])->setInc('total_consume',$data['num']);
            $result = array(
                'resultcode' => 40000,
                'reason' => '记录成功！',
            );
            print json_encode($result);
        }else{
            $result = array(
                'resultcode' => 40004,
                'reason' => '记录失败！',
            );
            print json_encode($result);
        }
    }
    //用户胜负接口
    function Outcomeapi() {
        $openid = I('param.uid');
        $score = intval(I('param.record'));
        if ($score>0) {
            $type=1;
        }else if($score<0){
            $type=2;
        }else{
            $type=3;
        }
        if ($openid == "") {
            $result = array(
                'resultcode' => '50001',
                'reason' => '用户ID为空'
            );
            print json_encode($result);
        } else {
            $Gamer = M('Gamer');
            $re = $Gamer->where('openid=' . $openid)->find();
            if ($re) {
                $record = M('Record');
                $data['openid'] = $openid;
                $data['score'] = $score;
                $data['type'] = $type;
                $data['addtime'] = time();
                $r=$record->add($data);
                if ($r) {
                    //游戏总局数累加
                    $Gamer->where('openid=' . $openid)->setInc('total_play',1);
                    $result = array(
                        'resultcode' => 50000,
                    'reason' => '日志记录成功！'
                    );
                    print json_encode($result);
                }else{
                    $result = array(
                        'resultcode' => 50003,
                        'reason' => '记录失败！'
                    );
                    print json_encode($result);
                }
            }
        }
    }
}